<template>
  <el-dialog v-model="visible" :title="!dataForm.skuId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <el-form-item label="关联的菜品ID" prop="dishId">
        <el-input v-model="dataForm.dishId" placeholder="关联的菜品ID"></el-input>
      </el-form-item>
      <el-form-item label="口味" prop="flavor">
        <el-input v-model="dataForm.flavor" placeholder="口味"></el-input>
      </el-form-item>
      <el-form-item label="规格（JSON格式）" prop="specification">
        <el-input v-model="dataForm.specification" placeholder="规格（JSON格式）"></el-input>
      </el-form-item>
      <el-form-item label="库存数量" prop="stockQuantity">
        <el-input v-model="dataForm.stockQuantity" placeholder="库存数量"></el-input>
      </el-form-item>
      <el-form-item label="库存预警阈值" prop="stockThreshold">
        <el-input v-model="dataForm.stockThreshold" placeholder="库存预警阈值"></el-input>
      </el-form-item>
      <el-form-item label="售价" prop="price">
        <el-input v-model="dataForm.price" placeholder="售价"></el-input>
      </el-form-item>
      <el-form-item label="成本价" prop="cost">
        <el-input v-model="dataForm.cost" placeholder="成本价"></el-input>
      </el-form-item>
      <el-form-item label="SKU图片URL（如有不同）" prop="imageUrl">
        <el-input v-model="dataForm.imageUrl" placeholder="SKU图片URL（如有不同）"></el-input>
      </el-form-item>
      <el-form-item label="SKU状态" prop="status">
        <ren-select v-model="dataForm.status" dict-type="sku_status" placeholder="状态"></ren-select>
        <!-- <el-input v-model="dataForm.status" placeholder="SKU状态"></el-input> -->
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalStatus">
        <el-input v-model="dataForm.approvalStatus" placeholder="审批状态"></el-input>
      </el-form-item>
      <el-form-item label="促销活动信息（JSON格式）" prop="promotionInfo">
        <el-input v-model="dataForm.promotionInfo" placeholder="促销活动信息（JSON格式）"></el-input>
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input v-model="dataForm.supplierId" placeholder="供应商ID"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  skuId: "",
  dishId: "",
  flavor: "",
  specification: "",
  stockQuantity: "",
  stockThreshold: "",
  price: "",
  cost: "",
  imageUrl: "",
  status: "",
  approvalStatus: "",
  promotionInfo: "",
  version: "",
  createdAt: "",
  updatedAt: "",
  supplierId: "",
});

const rules = ref({
});

const init = (skuId?: number) => {
  visible.value = true;
  dataForm.skuId = skuId;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (skuId) {
    getInfo(skuId);
  }
};

// 获取信息
const getInfo = (skuId: number) => {
  baseService.get("/sys/skus/" + skuId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.skuId ? baseService.post : baseService.put)("/sys/skus", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>